<?php
/**
 * Created by PhpStorm.
 * User: Lin
 * Date: 2017/2/27 0027
 * Time: 下午 17:51
 */
Class LoginAction extends Action{
    
    //登入首页视图
    public function index(){
        $this->display();
    }
    
    //运行登入验证
    public function login(){
        if(!IS_POST){//禁止非表单提交访问
            halt('页面不存在');
        }
        if(I('code','','md5')==$_SESSION['verify']){
            $db=M('member');
            $user=$db->where(array('username'=>I('username')))->find();
            if ($user&&I('password','','md5')==$user['password']){
                session('userId',$user['id']);
                session('username',$user['username']);
                redirect(U(GROUP_NAME.'/Index'));
            }else{
                $this->error('用户不存在或密码错误');
            }
        }else{
            $this->error('验证码错误');
        }
    }

    //注册
    public function register(){
        $this->display();
    }

    //运行注册
    public function runRegister(){
        if(!IS_POST){//禁止非表单提交访问
            halt('页面不存在');
        }
        if(I('password','','intval')!=I('password2','','intval')){
            $this->error('密码不一致',U(GROUP_NAME.'/Login/register'));
        }
        if(I('code','','md5')==$_SESSION['verify']){
            $db=M('member')->where(array('username'=>I('username')))->find();
            if ($db==false){
                $date=array(
                    'username'=>I('username'),
                    'password'=>I('password','','md5')
                );
                M('member')->add($date);
                redirect(U(GROUP_NAME.'/Login/index'));
            }else{
                $this->error('用户已经存在');
            }
        }else{
            $this->error('验证码错误');
        }
    }

    //验证码图片
    Public function verify(){
        import('ORG.Util.Image');
        Image::buildImageVerify();
    }
}